﻿using EnvDTE;
using EnvDTE80;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace bleissem.CMWVS.Helpers
{
    public class VisualStudioHelper
    {
        public static IEnumerable<Project> GetProjects(Solution solution)
        {
            List<Project> projects = new List<Project>();

            object[] projobjs = (object[])solution.SolutionBuild.StartupProjects;
            foreach (object projobj in projobjs)
            {
                projects.Add(solution.Item(projobj));
            }

            return projects;
        }

        private static Property GetStartArgumentProperty(Project project)
        {
            return (from property in project.ConfigurationManager.ActiveConfiguration.Properties.OfType<Property>() where "StartArguments".Equals(property.Name) select property).FirstOrDefault();
        }

        public static string GetStartArguments(Project project)
        {
            Property startargprop = GetStartArgumentProperty(project);

            string startArgs = string.Empty;
            if (null != startargprop)
            {
                startArgs = startargprop.Value.ToString();
            }

            return startArgs;
        }

        private static Property GetProperty(Properties properties, string key)
        {
            return (from property in properties.OfType<Property>() where key.Equals(property.Name) select property).FirstOrDefault();
        }

        private static Property GetOutputFileNameProperty(Project project)
        {
            return GetProperty(project.Properties, "OutputFileName");
        }

        private static Property GetStartProgramProperty(Project project)
        {
            return GetProperty(project.ConfigurationManager.ActiveConfiguration.Properties, "StartProgram");
        }

        private static Property GetStartActionProperty(Project project)
        {
            return GetProperty(project.ConfigurationManager.ActiveConfiguration.Properties, "StartAction");
        }

        private static Property GetOutputPathProperty(Project project)
        {
            return GetProperty(project.ConfigurationManager.ActiveConfiguration.Properties, "OutputPath");
        }

        public static string GetToRun(Project project)
        {
            Property startActionProp = GetStartActionProperty(project);

            int startActionValue = 0;
            if (!int.TryParse(startActionProp.Value.ToString(), out startActionValue))
            {
                throw new NotSupportedException("unowkn Value");
            }

            switch (startActionValue)
            {
                case 0:
                    {
                        Property propFileName = GetOutputFileNameProperty(project);
                        Property propOutputPath = GetOutputPathProperty(project);
                        return Path.Combine(propOutputPath.Value.ToString(), propFileName.Value.ToString());
                    }
                case 1:
                    {
                        Property prop = GetStartProgramProperty(project);
                        return prop.Value.ToString();
                    }
                case 2:
                    {
                        throw new NotSupportedException("browser url:");
                    }
                default:
                    {
                        throw new NotSupportedException("unowkn value");
                    }
            }
        }

        public static DirectoryInfo ProjectFolderDir(Project project)
        {
            return new DirectoryInfo(Path.GetDirectoryName(project.FullName));
        }
    }
}